const Koa = require('koa');

const app = new Koa();

const static = require('koa-static');

const path = require('path');

const bodyparser = require('koa-bodyparser');

const router = require('koa-router')();

const query = require('./db/index');

const hmc = require('./util/hmc');

//挂载中间件
//静态资源
app.use(static(path.join(__dirname,'./public')));

//处理前端post/put传递的参数
app.use(bodyparser());

//挂在路由中间件
app.use(router.routes());

app.use(router.allowedMethods());

//注册接口
router.post('/api/registry',async ctx => {
    //1.获取参数
    let {phone,password} = ctx.request.body;
    //2.容错处理，参数校验
    if(/^\d{11}$/.test(phone) && /^[0-9a-zA-Z]{6,20}$/.test(password)){
        //3.排重
        let target = await query('select * from userlist where phone=?',[phone]);
        if(target.length){
            ctx.body = {
                code:3,
                msg:'此手机号已被使用'
            }
        }else{
            //4.注册
            try{
                let hmcPwd = hmc(password);  //加密密码
                await query('insert into userlist (phone,password) values (?,?)',[phone,hmcPwd]);
                ctx.body = {
                    code:1,
                    msg:'注册成功'
                }
            }catch(e){
                ctx.body = {
                    code:0,
                    msg:e
                }
            }
        }
    }else{
        ctx.body = {
            code:0,
            msg:'参数有误'
        }
    }
})

//登录接口
router.post('/api/login',async ctx => {
    //1.接收参数
    let {phone,password} = ctx.request.body;
    //2.容错处理，参数校验
    if(/^\d{11}$/.test(phone) && /^[0-9a-zA-Z]{6,20}$/.test(password)){
        let hmcPwd = hmc(password);
        let target = await query('select * from userlist where phone=? and password=?',[phone,hmcPwd]);
        if(target.length){
            //登录成功
            ctx.body = {
                code:1,
                msg:'登录成功'
            }
        }else{
            //登录失败
            ctx.body = {
                code:0,
                msg:'登录失败'
            }
        }
    }else{
        ctx.body = {
            code:0,
            msg:'参数有误'
        }
    }
})

//签到的接口
router.post('/api/sign',async ctx => {
    //1.接收前端传递的参数
    let {phone} = ctx.request.body;
    if(/^\d{11}$/.test(phone)){
        let y = new Date().getFullYear();
        let m = new Date().getMonth()+1;
        let d = new Date().getDate();
        let timer = `${y}-${m}-${d}`;
        //2.每天只能签到一次
        let target = await query('select * from signlist where phone=? and timer=?',[phone,timer]);
        if(target.length){
            //已经签到
            ctx.body = {
                code:3,
                msg:'已经签到'
            }
        }else{
             //签到
            try{
            
                await query('insert into signlist (phone,timer) values (?,?)',[phone,timer]);
                ctx.body = {
                    code:1,
                    msg:'签到成功'
                }
            }catch(e){
                //登录失败
                ctx.body = {
                    code:0,
                    msg:e
                }
            }
        }
    }else{
        ctx.body = {
            code:0,
            msg:'参数有误'
        }
    }
})

//查询是否签到
router.get('/api/isSign',async ctx => {
    let {phone} = ctx.query;
    let y = new Date().getFullYear();
    let m = new Date().getMonth()+1;
    let d = new Date().getDate();
    let timer = `${y}-${m}-${d}`;
    if(/^\d{11}$/.test(phone)){
        let target = await query('select * from signlist where phone=? and timer=?',[phone,timer]);
        console.log(target);
        if(target.length){
            ctx.body = {
                code:1,
                msg:'已签到'
            }
        }else{
            ctx.body = {
                code:2,
                msg:'未签到'
            }
        }
    }else{
        ctx.body = {
            code:0,
            msg:'参数有误'
        }
    }
})

//获取签到天数
router.get('/api/days',async ctx => {
    let {phone} = ctx.query;
    if(/^\d{11}$/.test(phone)){
        let count = await query('select count(*) from signlist where phone=?',[phone]);
        console.log(count);
        ctx.body = {
            code:1,
            count:count[0]['count(*)']
        }
    }else{
        ctx.body = {
            code:0,
            msg:'参数有误'
        }
    }
})

app.listen(3000,() => {
    console.log("服务启动成功")
})